package com.szholly.data.general.metadata.table;

import java.util.HashMap;

import org.springframework.stereotype.Component;

import com.szholly.utils.spring.SpringBeanFactory;
import com.szholly.data.general.*;
import com.szholly.data.general.metadata.IBaseTable;

/**
 * 结构信息表
 */
@Component("DATA_TABLE")
public class Tables extends InterceptorTableBase  implements IBaseTable{

	public final static String TABLE_NAME = "DATA_TABLE";

	/**
	 * 获取单例对象
	 */
	public static Tables getSingleRef() {
		return (Tables) SpringBeanFactory.getBean("DATA_TABLE");
	}

	@Override
	public String getMetadataTableName() {
		return TABLE_NAME;
	}

	@Override
	public String getMetadataTableAliasName() {
		return "元数据表";
	}
	
	@Override
	public String getCompanyName(){
		return null;
	}

	private HashMap<String, Field> fields;

	@Override
	protected HashMap<String, Field> fields() {
		if (fields == null) {
			fields = new HashMap<String, Field>();

			Field itemField = new Field();
			itemField.setFieldName(RowBase.FIELD_GUID);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(1);
			itemField.setFieldAliasName("全局ID");
			itemField.setCanShow(false);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(RowBase.FIELD_VERSION);
			itemField.setDataType(GlobalDataType.Int32);
			itemField.setFieldID(2);
			itemField.setFieldAliasName("数据版本");
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(RowBase.FIELD_COMPANY);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(3);
			itemField.setFieldAliasName("企业名称");
			itemField.setCanShow(false);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(RowBase.FIELD_SYSTEMNAME);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(3);
			itemField.setFieldAliasName("系统名称");
			itemField.setCanShow(false);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);

			itemField = new Field();
			itemField.setFieldName(TablesRow.FIELD_TABLEID);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(2);
			itemField.setFieldAliasName("表ID");
			itemField.setControlWidth(80);
			itemField.setIsKey(true);
			itemField.setCanShow(true);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);

			itemField = new Field();
			itemField.setFieldName(TablesRow.FIELD_TABLEALIASNAME);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(3);
			itemField.setFieldAliasName("表别名");
			itemField.setControlWidth(100);
			itemField.setCanShow(true);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);

			itemField = new Field();
			itemField.setFieldName(TablesRow.FIELD_TREETABLEFIELD);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(4);
			itemField.setFieldAliasName("树型表");
			itemField.setControlWidth(100);
			itemField.setCanShow(true);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);

			itemField = new Field();
			itemField.setFieldName(TablesRow.FIELD_SYSFIELDS);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(5);
			itemField.setFieldAliasName("系统字段名称");
			itemField.setControlWidth(100);
			itemField.setCanShow(true);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);

			itemField = new Field();
			itemField.setFieldName(TablesRow.FIELD_TABLETYPE);
			itemField.setDataType(GlobalDataType.Int32);
			itemField.setFieldID(6);
			itemField.setFieldAliasName("表类型");
			itemField.setControlWidth(100);
			itemField.setCanShow(true);
			IDomain pDomain = new MemDomain(TableTypeDic.getDataDic(), "(元数据)表类型");
			itemField.setDomain(pDomain);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);

			itemField = new Field();
			itemField.setFieldName(TablesRow.FIELD_TABLEGROUP);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(7);
			itemField.setFieldAliasName("表分组");
			itemField.setControlWidth(100);
			itemField.setCanShow(true);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);

			itemField = new Field();
			itemField.setFieldName(TablesRow.FIELD_SPATIALREFERENCE);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(8);
			itemField.setFieldAliasName("空间参考信息");
			itemField.setControlWidth(100);
			itemField.setCanShow(true);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);

			itemField = new Field();
			itemField.setFieldName(TablesRow.FIELD_MANAGERCODETABLE);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(8);
			itemField.setFieldAliasName("管理编码表");
			itemField.setControlWidth(100);
			itemField.setCanShow(true);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);

		}
		return fields;
	}

	@Override
	public String getTableGroup() {
		return "Data框架";
	}

	private RelationNode relationNode;

	/**
	 * 表关系
	 */
	@Override
	public RelationNode getRelation() {
		if (relationNode == null) {
			relationNode = new RelationNode(TABLE_NAME);

			// 表关系表
			RelationInfo relationInfo = new RelationInfo();
			relationInfo.Foreignkey = new HashMap<String, String>();
			relationInfo.Foreignkey.put(TablesRow.FIELD_TABLEID,
					TableRelationRow.FIELD_PTABLEID);
			relationInfo.RelationStrength = RelationStrength.UpdateChild;
			relationInfo.RelationType = RelationType.OneToMore;
			relationInfo.metadataTableAliasName = "表关系表";
			relationNode.getChildRelationNode().put(
					TableRelationTable.TABLE_NAME, relationInfo);

			// 结构表
			relationInfo = new RelationInfo();
			relationInfo.Foreignkey = new HashMap<String, String>();
			relationInfo.Foreignkey.put(TablesRow.FIELD_TABLEID,
					TableMetadataRow.FIELD_TABLEID);
			relationInfo.RelationStrength = RelationStrength.UpdateChild;
			relationInfo.RelationType = RelationType.OneToMore;
			relationInfo.metadataTableAliasName = "元数据表";
			relationNode.getChildRelationNode().put(
					TableMetadataTable.TABLE_NAME, relationInfo);

			// 注册表
			relationInfo = new RelationInfo();
			relationInfo.Foreignkey = new HashMap<String, String>();
			relationInfo.Foreignkey.put(TablesRow.FIELD_TABLEID,
					DataRegRow.FIELD_TABLEID);
			relationInfo.RelationStrength = RelationStrength.UpdateChild;
			relationInfo.RelationType = RelationType.OneToMore;
			relationInfo.metadataTableAliasName = "数据注册表";
			relationNode.getChildRelationNode().put(DataRegTable.TABLE_NAME,
					relationInfo);

		}
		return relationNode;
	}

}
